package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void update(CategoryDTO dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);
        // category.setUpdateTime(LocalDateTime.now());
        // category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    @Override
    public PageResult getPage(CategoryPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<Category> categoryPage = categoryMapper.getPage(dto);
        long total = categoryPage.getTotal();
        List<Category> records = categoryPage.getResult();
        return new PageResult(total,records);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        // .updateTime(LocalDateTime.now()).updateUser(BaseContext.getCurrentId())
        Category category = Category.builder().status(status).id(id).build();
        categoryMapper.updateStatus(category);
    }

    @Override
    public void insert(CategoryDTO dto) {
        // .createTime(LocalDateTime.now()).updateTime(LocalDateTime.now())
        //         .createUser(BaseContext.getCurrentId()).updateUser(BaseContext.getCurrentId())
        Category category = Category.builder().status(StatusConstant.DISABLE).build();
        BeanUtils.copyProperties(dto,category);
        categoryMapper.insert(category);
    }

    @Override
    public List<Category> getByType(Integer type) {
        return categoryMapper.getByType(type);
    }

    @Override
    public PageResult getCondition(CategoryPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<Category> categoryPage = categoryMapper.getCondition(dto);
        long total = categoryPage.getTotal();
        List<Category> records = categoryPage.getResult();
        return new PageResult(total,records);
    }

    @Override
    public void delete(Long id) {
        Integer count1 = dishMapper.countByCategoryId(id);
        if (count1 > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        Integer count2 = setmealMapper.countByCategoryId(id);
        if (count2 > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.delete(id);
    }

    @Override
    public List<Category> getCategoryByType(Integer type) {
        return categoryMapper.getByType(type);
    }

}







































